using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHI.DSS.WebFoundation;
using DHI.Projects.TangJTWest.Interfaces;
using DHI.DSS.WebFoundation.Data;

namespace DHI.Projects.TangJTWest.Data
{
    public class SystemLogInfoWritingDAO : WritableDAO<SystemLogInfo>
    {
        public static DbManager _dbManager = DbManager.FromConfig("DefaultConnection");

        public SystemLogInfoWritingDAO()
            : base(_dbManager)
        {
            
        }

        public SystemLogInfoWritingDAO(DbManager dbManager)
            : base(dbManager)
        {
            _dbManager = dbManager;
        }

        public bool AddSystemLogInfo(SystemLogInfo[] items)
        {
            var returnResult = false;
            try
            {
                this.Add(items);
                returnResult = true;
            }
            catch (Exception ex)
            {
                this.GetType().LogError(ex, "[AddSystemLogInfo, Exception found.]");
                throw new DHI.DSS.WebFoundation.Exceptions.DataOpeartionFailedException(ex.Message);
            }
            return returnResult;
        }

        public bool UpdateSystemLogInfo(SystemLogInfo item)
        {
            var returnResult = false;
            try
            {
                returnResult = this.Update(item);
            }
            catch (Exception ex)
            {
                this.GetType().LogError(ex, "[UpdateSystemLogInfo, Exception found.]");
                throw new DHI.DSS.WebFoundation.Exceptions.DataOpeartionFailedException(ex.Message);
            }
            return returnResult;
        }

        public bool DeleteSystemLogInfo(SystemLogInfo item)
        {
            var returnResult = false;
            try
            {
                returnResult = this.Delete(item);
            }
            catch (Exception ex)
            {
                this.GetType().LogError(ex, "[DeleteSystemLogInfo, Exception found.]");
                throw new DHI.DSS.WebFoundation.Exceptions.DataOpeartionFailedException(ex.Message);
            }
            return returnResult;
        }

        public bool DeleteSystemLogInfoByLogTypes(string[] logTypes)
        {
            var returnResult = false;
            try
            {
                foreach (var logType in logTypes)
                {
                    var excutor = new DynamicalBatchExcutor<SystemLogInfo>(this.DBManager);
                    excutor.Query().Filter().Eq(r => r.LogType, logType);
                    excutor.BatchDelete(this.Transaction, _TimeOut);
                }
                returnResult = true;
            }
            catch (Exception ex)
            {
                this.GetType().LogError(ex, "[DeleteSystemLogInfoByLogTypes, Exception found.]");
                throw new DHI.DSS.WebFoundation.Exceptions.DataOpeartionFailedException(ex.Message);
            }

            return returnResult;
        }

        public string InsertBatch<T>(IEnumerable<T> entityList)
            where T : class
        {
            return (new SqlBatchWritingDAO<T>(_dbManager)).InsertBatch(entityList);
        }

        public string UpdateBatch<T>(IEnumerable<T> entityList)
            where T : class
        {
            return (new SqlBatchWritingDAO<T>(_dbManager)).UpdateBatch(entityList);
        }
    }
}